
## V  Figure 3  ------------------------
fig_MonthlySpending_byPhat_andDeath <- function(data_all = dt_for_exhibits_all,
                       data_cnr = dt_for_exhibits_cancer,
                       figs_type ="presentation", 
                       sample_n = "cancer") {
  
  
  dt_prob_for_density_by_mort <- 
    rbind(
      data_all[DMG_died_within_365d=="1", 
               .(prob_for_report, 
                 sample="General Population Sample",
                 group="Decedent")],
      data_all[DMG_died_within_365d=="0", 
               .(prob_for_report, 
                 sample="General Population Sample",
                 group="Survivor")],
      data_cnr[DMG_died_within_365d=="1", 
               .(prob_for_report, 
                 sample="Cancer Sample",
                 group="Decedent")],
      data_cnr[DMG_died_within_365d=="0", 
               .(prob_for_report, 
                 sample="Cancer Sample",
                 group="Survivor")]
    )[, `:=` (
      sample = factor(sample, levels=c("General Population Sample", "Cancer Sample")),
      group = factor(group, levels=c("Decedent", "Survivor"))
    )]
  
  # build the data of monthly average spending
  monthly_cost_func <- function(data, sample_txt, group_txt, weighting) {
    output_dt <- 
      data[
        ,
        .(sample = sample_txt,
          group = group_txt,
          ave_monthly_cost = (sum(UTL_f365d_total_cost)/sum(num_days_lived))*31,
          obs_num = .N),
        by = .(bins = plyr::round_any(prob_for_report, 0.1, ceiling))
        ][obs_num >= min_obs_num][, bins := bins-0.1]
    return(output_dt)
  }
  
  dt_monthly_cost <- rbind(
    monthly_cost_func(data_all[DMG_died_within_365d == "1"],
                      "General Population Sample",
                      "Decedent"),
    monthly_cost_func(data_all[DMG_died_within_365d == "0"],
                      "General Population Sample",
                      "Survivor"),
    monthly_cost_func(data_cnr[DMG_died_within_365d == "1"],
                      "Cancer Sample",
                      "Decedent"),
    monthly_cost_func(data_cnr[DMG_died_within_365d == "0"],
                      "Cancer Sample",
                      "Survivor")
  )[
    , `:=` (
      sample = factor(sample, levels=c("General Population Sample", "Cancer Sample")),
      group = factor(group, levels=c("Decedent", "Survivor"))
    )
    ]
  
  if (sum(dt_monthly_cost[sample == "Cancer Sample"]$obs_num)/
      nrow(data_cnr)<0.99 |
      sum(dt_monthly_cost[sample == "General Population Sample"]$obs_num)/
      nrow(data_all)<0.99) {
    stop("Problem with building dt for figure 4!")
  }
  
  # max y axis - monthly cost
  max_y_axis <- 
    plyr::round_any(max(dt_monthly_cost$ave_monthly_cost), 5000, ceiling)
  
  # labels 
  text_dt <- rbind(
    data.table::data.table(
      sample = c(rep("General Population Sample", 3), rep("Cancer Sample", 3)),
      group = rep(c("Decedent", "Survivor", NA_character_), 2),
      lab = rep(c("Decedent", 
                  "Survivor", 
                  "Scaled Density\n(Dark = Survivor / Light = Decedent)"), 2),
      x = c(.55,.55,.5,.5,.5,.7),
      y = c(0.53, 0.25, 0.05, 0.72, 0.4, 0.05),
      size_col = rep(c(inplot_text_size+2,inplot_text_size+2,inplot_text_size+2),2),
      type = "presentation"
    ),
    data.table::data.table(
      sample = c(rep("General Population Sample", 3), rep("Cancer Sample", 3)),
      group = rep(c("Decedent", "Survivor", NA_character_), 2),
      lab = rep(c("Decedent\n Spending", 
                  "Survivor\n Spending", 
                  "Scaled Density\n(Dark = Survivor / Light = Decedent)"), 2),
      x = rep(0.55, 6),
      y = c(0.48, 0.25, 0.1, 0.75, 0.4, 0.1),
      size_col = rep(c(inplot_text_size+2,inplot_text_size+2,inplot_text_size+2),2),
      type = "paper"
    )
  )[, `:=` (
    sample = factor(sample, levels=c("General Population Sample", "Cancer Sample")),
    group = factor(group, levels=c("Decedent", "Survivor"))
  )]
  
  if (sample_n == "all") {
    sample_full_n = "General Population Sample"
  } else {
    sample_full_n = "Cancer Sample"
  }
  
  
  write.csv(dt_monthly_cost[sample==sample_full_n] , 
            file = paste0("MonthlySpending_byPhat_andDeath_", 
                    sample_n, "_", figs_type, ".csv"))
  
  pdf(file = paste0("MonthlySpending_byPhat_andDeath_", 
                    sample_n, "_", figs_type, ".pdf"),width = 7.5, height = 7.5)
  print(
    ggplot() +
      geom_density(data = dt_prob_for_density_by_mort[sample==sample_full_n], 
                   aes(x=prob_for_report, y=..scaled.., fill=group),
                   size = 0.7,
                   color=NA,
                   alpha = 0.2) +
      geom_line(data = dt_monthly_cost[sample==sample_full_n],
                aes(x=bins,
                    y=ave_monthly_cost/max_y_axis,
                    linetype = group),
                size=1) +
      geom_text(
        data    = text_dt[sample==sample_full_n & type == figs_type],
        mapping = aes(x = x, y = y, size = 4 ,
                      label = lab) #,
      ) +
      expand_limits(x = c(0,1))+
      scale_x_continuous(breaks = seq(0, 0.9, 0.1)) +
      scale_y_continuous(labels = function(x) {
        scales::comma(round(x*max_y_axis))
      },
      breaks = seq(0, 1, 1/(max_y_axis/5000))) +
      scale_alpha_discrete(guide=FALSE) +
      scale_color_grey(guide=FALSE) +
      scale_linetype_manual(values = c("dashed","solid"),guide=FALSE)+
      scale_fill_manual(values = c("grey60","grey5"),guide=FALSE)+
      labs(x = "Initial Prognosis (One-Year Mortality Risk)", 
           y =  "Average Monthly Spending (NIS)") + 
      theme(aspect.ratio = 1.2, 
            legend.position = "none",
            text = element_text(size = 24)) 
  )
  dev.off()
  
  
}
